﻿using AutoupdateClient.Mgr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoupdateClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Mgr.ConfigMgr.getInstance().Load();

            Mgr.ClientMgr.getInstance().ConnectToServer();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DownloadMgr.getInstance().workMode == "downloading")
            {
                this.Invoke(new Action(() =>
               {
                   this.lblFileName.Text = "正在下载 " + DownloadMgr.getInstance().currrentFileName;

                   this.progressBar1.Maximum = (int)DownloadMgr.getInstance().currentFileLength;

                   this.progressBar1.Value = (int)DownloadMgr.getInstance().offset;

                   this.progressBar2.Maximum = (int)DownloadMgr.getInstance().downloadFileCount;

                   this.progressBar2.Value = (int)DownloadMgr.getInstance().downloadedFileIndex;

                   this.lblNotice.Text = "正在更新文件，请稍候...";
               }));
            }
            else if (DownloadMgr.getInstance().workMode == "starting")
            {
                this.Invoke(new Action(() =>
                {
                    this.lblNotice.Text = "自动更新程序正在启动，请稍候...";
                }));
            }
            else
            {

                this.Invoke(new Action(() =>
                {
                    this.progressBar1.Value = (int)DownloadMgr.getInstance().currentFileLength;

                    this.progressBar2.Value = (int)DownloadMgr.getInstance().downloadFileCount;

                    this.lblNotice.Text = "所有文件已更新完成";

                    this.timer1.Stop();

                    System.Threading.Thread.Sleep(1000);

                    if (!string.IsNullOrEmpty(Mgr.ConfigMgr.getInstance().config.executeFile))
                    {
                        this.lblNotice.Text = "正在启动指定的程序";

                        string exeFile = Environment.CurrentDirectory;

                        if (!string.IsNullOrEmpty(Mgr.ConfigMgr.getInstance().config.executePath))
                        {
                            exeFile += "\\" + Mgr.ConfigMgr.getInstance().config.executePath + "\\" + Mgr.ConfigMgr.getInstance().config.executeFile;
                        }

                        if (!string.IsNullOrEmpty(exeFile))
                        {
                            System.Diagnostics.ProcessStartInfo pinfo = new System.Diagnostics.ProcessStartInfo();

                            pinfo.UseShellExecute = true;

                            pinfo.FileName = exeFile;
                            //启动进程

                            System.Diagnostics.Process p = System.Diagnostics.Process.Start(pinfo);

                            System.Threading.Thread.Sleep(2000);

                            if (p.Id != 0)
                            {
                                Environment.Exit(1);
                            }
                        }
                        else
                        {
                            Environment.Exit(1);
                        }


                    }
                }));
            }
        }
    }
}
